package com.impossible.chan;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class MergePriceUtil {

    public static List<BaseDing> getTbList(List<MergeK> priceList){
        List<MergeK> mergeKList = mergeList(priceList);
        return toTbList(mergeKList);
    }

    public static Boolean compareKContain(MergeK k1, MergeK k2){
        if(k1.getH().compareTo(k2.getH()) >= 0
                && k1.getL().compareTo(k2.getL()) <= 0 ){
            return true;
        }

        if(k1.getH().compareTo(k2.getH()) <= 0
                && k1.getL().compareTo(k2.getL()) >= 0 ){
            return true;
        }
        return false;
    }

    public static MergeK mergeK(MergeK k1, MergeK k2, MergeK k0){
        if(k0.getH().compareTo(k1.getH()) < 0){
            return mergeKUp(k1, k2);
        }else {
            return mergeKDown(k1, k2);
        }
    }

    public static MergeK mergeKUp(MergeK k1, MergeK k2){
        MergeK result = new MergeK();
        result.setTs(k1.getTs());
        result.setH(k1.getH());
        result.setL(k1.getL());
        if(k1.getH().compareTo(k2.getH()) <= 0){
            result.setH(k2.getH());
        }
        if(k1.getL().compareTo(k2.getL()) <= 0){
            result.setL(k2.getL());
        }
        result.setTs(k2.getTs());
        return result;
    }

    public static MergeK mergeKDown(MergeK k1, MergeK k2){
        MergeK result = new MergeK();
        result.setTs(k1.getTs());
        result.setH(k1.getH());
        result.setL(k1.getL());
        if(k1.getH().compareTo(k2.getH()) >= 0){
            result.setH(k2.getH());
        }
        if(k1.getL().compareTo(k2.getL()) >= 0){
            result.setL(k2.getL());
        }
        result.setTs(k2.getTs());
        return result;
    }

    public static List<MergeK> mergeList(List<MergeK> priceList){
        List<MergeK> resultList = new ArrayList();
        for(MergeK mergeK : priceList){
            if(resultList.size() <= 1){
                resultList.add(mergeK);
                continue;
            }
            MergeK pro_item = resultList.get(resultList.size() - 2);
            MergeK k_item = resultList.get(resultList.size() - 1);
            if(compareKContain(k_item, mergeK)){
                MergeK new_item = mergeK(k_item, mergeK, pro_item);
                resultList.remove(resultList.size()-1);
                resultList.add(new_item);
            }else {
                resultList.add(mergeK);
            }
        }
        return resultList;
    }

    public static List<BaseDing> toTbList(List<MergeK> mergeList){
        List<BaseDing> resultList = new ArrayList();
        if(mergeList.size() < 3){
            return resultList;
        }

        String cs = "";
        int lastIndex = -2;
        for(int i =1 ;i<mergeList.size()-1;i++){
            //顶分处理
            if(mergeList.get(i).getH().compareTo(mergeList.get(i-1).getH()) > 0
                    && mergeList.get(i).getH().compareTo(mergeList.get(i+1).getH()) > 0){
                if(!cs.equals("top")){
                    if(resultList.size() == 0){
                        BaseDing thisDing = createBaseDing(mergeList.get(i).getTs(), mergeList.get(i).getH(), 1);
                        cs = "top";
                        resultList.add(thisDing);
                        lastIndex = i;
                    }else {
                        if(i - lastIndex > 3){
                            BaseDing thisDing = createBaseDing(mergeList.get(i).getTs(), mergeList.get(i).getH(), 1);
                            cs = "top";
                            resultList.add(thisDing);
                            lastIndex = i;
                        }
                    }
                }else {
                    if(mergeList.get(i).getH().compareTo(resultList.get(resultList.size()-1).getValue())>0){
                        BaseDing thisDing = createBaseDing(mergeList.get(i).getTs(), mergeList.get(i).getH(), 1);
                        cs = "top";
                        resultList.remove(resultList.size()-1);
                        resultList.add(thisDing);
                        lastIndex = i;
                    }
                }
            }

            //底分处理
            if(mergeList.get(i).getL().compareTo(mergeList.get(i-1).getL()) < 0
                    && mergeList.get(i).getL().compareTo(mergeList.get(i+1).getL()) < 0){
                if(!cs.equals("bottom")){
                    if(resultList.size() == 0){
                        BaseDing thisDing = createBaseDing(mergeList.get(i).getTs(), mergeList.get(i).getL(), 0);
                        cs = "bottom";
                        resultList.add(thisDing);
                        lastIndex = i;
                    }else {
                        if(i - lastIndex > 3){
                            BaseDing thisDing = createBaseDing(mergeList.get(i).getTs(), mergeList.get(i).getL(), 0);
                            cs = "bottom";
                            resultList.add(thisDing);
                            lastIndex = i;
                        }
                    }
                }else {
                    if(mergeList.get(i).getL().compareTo(resultList.get(resultList.size()-1).getValue())<0){
                        BaseDing thisDing = createBaseDing(mergeList.get(i).getTs(), mergeList.get(i).getL(), 0);
                        cs = "bottom";
                        resultList.remove(resultList.size()-1);
                        resultList.add(thisDing);
                        lastIndex = i;
                    }
                }
            }
        }

        return resultList;
    }

    public static BaseDing createBaseDing(Long ts, BigDecimal value, int dingType){
        BaseDing thisDing = new BaseDing();
        thisDing.setTs(ts);
        thisDing.setValue(value);
        thisDing.setDingType(dingType);
        return thisDing;
    }
}
